from rdm_sim import NetworkManagement as nm
from rdm_sim import NetworkProperties as np
from rdm_sim import Config as config
from rdm_sim import Topology
from Prob import P_NFR

#Calls the probe function to get the values for the monitorable metrics
def Monitor():
	active_links, bandwidth_consumption, write_time = nm.probe()
	return active_links, bandwidth_consumption, write_time

#Decides a Topology based on the monitorable metrics' values
def AnalyzeandPlan(f, previous_topology,
                   bandwidth_consumption, active_links, write_time,
                   P_MC, P_MR, P_MP,
                  ):
    #selected_topology = f(previous_topology, bandwidth_consumption, active_links, P_MC, P_MR)
    selected_topology = f(previous_topology, bandwidth_consumption, active_links)
    if selected_topology == True:
        Topology.setTopologyName(1)
    else:
        Topology.setTopologyName(0)
    S_belief = P_NFR([P_MC, P_MR, P_MP], "MST" if selected_topology else "RT")
    return S_belief

def basic_f(previous_topology, active_links, bandwidth_consumption, write_time):
	link_threshold = np.number_of_links*0.55
	bandwidth_threshold = (np.number_of_links*30)*0.31
	time_threshold = (np.number_of_links*20)*0.26

	if active_links < link_threshold:
		return False

	else:
		if bandwidth_consumption > bandwidth_threshold or write_time > time_threshold:
			return True

		else:
			return False

#Calls the effector function to set the topology for the network
def Execute():
	nm.effector()

#Calls all the previous functions
def Run(f, S):
	active_links, bandwidth_consumption, write_time = Monitor()
	previous_topology = nm.current_topology
    
	S_belief = AnalyzeandPlan(f, previous_topology, bandwidth_consumption, active_links, write_time, S[0], S[1], S[2]) # TODO: add surprise
	Execute()
	return S_belief